
import type { CommandResult, ICommand } from './types/core'

// 抽象命令基类
abstract class BaseCommand<T> implements ICommand<T> {
  constructor(
    public readonly service: string,
    public readonly method: string,
    public readonly params: any[] = [],
  ) {}

  async execute(): Promise<T> {
    try {
      const serviceInstance = ServiceRegistry.getInstance().resolve(this.service)
      const methodRef = (serviceInstance as any)[this.method]

      if (typeof methodRef !== 'function') {
        throw new Error(`Method ${this.method} not found on ${this.service}`)
      }

      return await methodRef.apply(serviceInstance, this.params)
    } catch (error) {
      // 统一的错误处理
      // ErrorHandler.handle(error)
      throw error
    }
  }
}
